package com.homedo.odin.solution.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.dao.EquipmentListMapper;
import com.homedo.odin.solution.enumcode.mng.ProjectPhaseStatusEnum;
import com.homedo.odin.solution.enumcode.usersafe.BizCodeEnum;
import com.homedo.odin.solution.enumcode.usersafe.UserSafeStatusEnum;
import com.homedo.odin.solution.model.entity.EquipmentList;
import com.homedo.odin.solution.model.entity.UserSafe;
import com.homedo.odin.solution.model.request.mng.MngEquipmentReqsListRequestBean;
import com.homedo.odin.solution.model.request.mng.MngEquipmentUpdateRequestBean;
import com.homedo.odin.solution.model.request.pc.PcEquipmentRequestBean;
import com.homedo.odin.solution.model.response.mng.MngEquipmentPageResponseBean;
import com.homedo.odin.solution.model.response.mng.MngEquipmentResponseBean;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p> 设备清单报备  服务实现类 </p>
 *
 * @author ZhangLei
 * @date 2018-05-22
 * @Description:
 */
@Service
public class EquipmentListServiceImpl extends ServiceImpl<EquipmentListMapper, EquipmentList> {

    @Autowired
    private UserSafeServiceImpl userSafeService;

    /**
     * 获取设备清单分页列表
     */
    public MngEquipmentPageResponseBean getEquipments(MngEquipmentReqsListRequestBean requestBean) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());
        List<MngEquipmentResponseBean> result = baseMapper
                .getEquipments(requestBean.getStartTime(), requestBean.getEndTime());
        updateProjectPhase(result);
        return BeanUtils.transfrom(MngEquipmentPageResponseBean.class, new PageInfo<>(result));
    }

    /**
     * 把项目阶段转换为名称
     */
    private void updateProjectPhase(List<MngEquipmentResponseBean> result) {
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(bean -> {
                if (null != bean.getProjectPhaseId()) {
                    bean.setProjectPhase(ProjectPhaseStatusEnum.getExistDisplayValue(bean.getProjectPhaseId()));
                }
            });
        }
    }

    /**
     * 修改跟踪记录
     */
    public void updateTrackRecord(Long equipmentId, MngEquipmentUpdateRequestBean requestBean) {
        EquipmentList equipmentList = baseMapper.selectById(equipmentId);
        if (null == equipmentList) {
            throw new BizException(Meta.CODE_451.getCode(), "设备清单不存在，equipmentId:" + equipmentId);
        }

        equipmentList.setTrackRecord(requestBean.getTrackRecord());
        equipmentList.setOperator(requestBean.getOperator());

        baseMapper.updateById(equipmentList);
    }

    /**
     * 添加设备清单报备
     */
    public void addEquipment(PcEquipmentRequestBean requestBean) {
        UserSafe safe = new UserSafe();
        safe.setBizCode(BizCodeEnum.PRODUCT.getDbValue());
        safe.setUserId(requestBean.getUserId());
        safe.setStatus(UserSafeStatusEnum.APPROVED.getDbValue());
        UserSafe userSafe = userSafeService.selectOne(safe);
        if (userSafe == null) {
            throw new BizException(Meta.CODE_451.getCode(), "未申请白名单，不能提交数据");
        }

        if (!ProjectPhaseStatusEnum.dbValueExist(requestBean.getProjectPhase())) {
            throw new BizException(Meta.CODE_451.getCode(),
                    "不存在该项目阶段, projectPhaseCode:" + requestBean.getProjectPhase());
        }

        EquipmentList equipmentList = new EquipmentList();

        BeanUtils.copyEntityProperties(requestBean, equipmentList);

        baseMapper.insert(equipmentList);
    }
}
